#include "common.h"
void read_command(char* command){
	fgets(command, 50, stdin);
	command[strlen(command)-1] = 0;
}
void interpret_command(char* command){
	int found = 0;
	if (strcmp(command, "help") == 0){
		list_commands();
		found = 1;
	}
	if (strcmp(command, "quit") == 0){
		printf("\nAre you sure? y/n \n");
		char c;		
		scanf("%c", &c);
		if (c == 'y' || c == 'Y') 
			exit(0);
		found = 1;
	}
	if (strcmp(command, "config") == 0){
		found = 1;
		char* server; 
		int port; 
		char* user;
		char* password;
		
		printf("\nServer address: ");
		server = (char*)calloc(250,sizeof(char)); 
		scanf("%s", server);
		
		printf("\nPort number (default 110): ");
		scanf("%d", &port);
		
		printf("\nUsername: ");
		user = (char*)calloc(50, sizeof(char));
		scanf("%s", user);		
		
		printf("\nPassword: ");
		password = (char*)calloc(50, sizeof(char));		
		scanf("%s", password);

		write_config(server, port, user, password);
		free(server);
		free(user);
		free(password);
	}
	if (strcmp(command, "receive") == 0){
		int socket = connect_to_server();
		char *data=(char*)calloc(1024,sizeof(char));
		recv_from_server(socket,data);
		send_comand(socket,"");
		close(socket);
		found = 1;
	}
	if (strcmp(command, "send") == 0){
		found = 1;
	}
	if (strcmp(command, "read from inbox") == 0){
		
		
		found = 1;
	}
}

void write_config(char* server, int port, char* user, char* password){
	FILE *f = fopen(CONFIG_FILE, "w");
	fprintf(f, "%s\n%d\n%s\n%s", server, port, user, password);
	fclose(f);
}

void prompt(){
	printf("pop3> ");
}

void list_commands(){
	printf("Command List: \n"
	       "help - provides this list of commands\n"
	       "config - edits the mail server configuration\n"
	       "quit - exists the program\n");
}

void read_config(char* server, int* port, char* user, char* password){
	FILE * f;
	if ((f=fopen(CONFIG_FILE, "r")) == NULL){
		printf("\nPlease check config file.\n");
	}
	else{
		fscanf(f, "%s%d%s%s", server,port,user, password);
		fclose(f);
	}
}
